import java.util.HashMap;
import java.util.Map;

class Solution {


    /**
     * 和为k的子数组个数  560题
     * @param nums
     * @param k
     * @return
     */
    public int subarraySum(int[] nums, int k) {
        Map<Integer,Integer> hash = new HashMap<>();
        hash.put(0,1);

        int sum = 0 ,count = 0;
        for(int i : nums){
            sum += i;
            count = hash.getOrDefault(sum-k,0);
            hash.put(sum, hash.getOrDefault(sum,0) + 1);
        }
        return count;
    }

    /** 除自身之外数值的积 238题
     *
     * @param nums
     * @return
     */

    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] f = new int[n];
        int[] g = new int[n];
        f[0] = 1;
        g[n - 1] = 1;

        for (int i = 1; i < n; i++)
            f[i] = f[i-1] * nums[i - 1];

        for(int i = n - 2; i >= 0 ; i --)
            g[i] = g[i+1] * nums[i+1];

        int[] arr = new int[n];
        for (int i = 0; i < n; i++)
            arr[i] = f[i] * g[i];

        return arr;
    }
}